package tool

import (
	"fmt"
	"os"
	"strconv"

	"math-clc/enum"

	"github.com/spf13/cobra"
)

func Error(cmd *cobra.Command, args []string, err error) {
	fmt.Fprintf(os.Stderr, "execute %s args:%v error:%v", cmd.Name(), args, err)
	os.Exit(1)
}

func ConvertArgsToFloat64Slice(args []string, errorHandling enum.ErrorHandling) []float64 {
	result := make([]float64, 0, len(args))
	for _, arg := range args {
		value, err := strconv.ParseFloat(arg, 64)
		if err != nil {
			switch errorHandling {
			case enum.ExitOnParseError:
				fmt.Fprintf(os.Stderr, "invalid number: %s\n", arg)
				os.Exit(1)
			case enum.PanicOnParseError:
				panic(err)
			default:
			}
		} else {
			result = append(result, value)
		}
	}

	return result
}
